package com.mljr.example.base;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.ctakit.sdk.SdkApplication;
import com.ctakit.sdk.util.JsonUtils;
import com.mljr.example.bean.AppVersion;
import com.mljr.example.bean.MyLocaction;
import com.mljr.example.service.UpdateManager;
import com.mljr.example.util.SharedPreferenceUtil;

import java.util.UUID;

public class Global {

    // 设备标识ID
    private static String uuid;

    private static long userId;

    private static String  userAgentClient;

    private static String  userAgentWebView;
    private static final String  userAgentName = "example";


    private static String macAddress;
    private static String accessToken;
    private static String imei;
    // 登录用的用户名（手机号）
    private static String userName;

    private static String servicePhone;

    private static AppVersion appNewVersion;
    // 最后登录时间
    private static long lastLoginTime = 0;

    private static Boolean isFirst;

    public static String defaultLocalBanner = "http://m.mljr.com/";

    /**
     * 开发模式 0-测试模式，1-开发模式
     */
    private static int devModel = -1;
    private static String pushToken;


    /**
     * 获取设置uuid,一般安装第一次使用时生成
     *
     * @return
     */
    public static String getUUID() {
        if (TextUtils.isEmpty(uuid)) {
            uuid = SharedPreferenceUtil.getStringFromSharedPreference(Config.Preferences.UUID, "");
            if (TextUtils.isEmpty(uuid)) {
                uuid = UUID.randomUUID().toString();
                SharedPreferenceUtil.putStringValueByKey(Config.Preferences.UUID, uuid);
            }
        }
        return uuid;

    }


    /**
     * 设置用户id
     */
    public static void setUserId(long id) {
        SharedPreferenceUtil.putLongValueByKey(Config.Preferences.USERID, id);
        userId = id;
    }

    /**
     * 获取用户id
     */
    public static long getUserId() {
        if (userId <= 0) {
            userId = SharedPreferenceUtil.getLongFromSharedPreference(Config.Preferences.USERID, 0);
        }
        return userId;
    }

    /**
     * 获取登录的用户名
     *
     * @return
     */

    public static String getUserName() {
        if (TextUtils.isEmpty(userName)) {
            userName = SharedPreferenceUtil.getStringFromSharedPreference(Config.Preferences.USERNAME, "");
        }
        return userName;

    }

    /**
     * 保用登录的用户名
     */
    public static void setUserName(String u) {
        SharedPreferenceUtil.putStringValueByKey(Config.Preferences.USERNAME, u);
        userName = u;
    }


    /**
     * 客服电话
     *
     * @param context
     * @return
     */

    public static String getServicePhone(final Context context) {
        if (TextUtils.isEmpty(servicePhone)) {
            servicePhone = SharedPreferenceUtil.getStringFromSharedPreference(Config.Preferences.SERVICEPHONE, "");
        }
        if (TextUtils.isEmpty(servicePhone)) {
            return "4000-959-888";
        }
        return servicePhone;

    }

    /**
     * 更新客服电话
     */
    public static void setServicePhone(String u) {
        SharedPreferenceUtil.putStringValueByKey(Config.Preferences.SERVICEPHONE, u);
        servicePhone = u;
    }


    public static String setAccessToken(String t) {
        SharedPreferenceUtil.putStringValueByKey(Config.Preferences.AccessToken, t);
        accessToken = t;
        return accessToken;

    }

    public static String getAccessToken() {
        if (TextUtils.isEmpty(accessToken)) {
            accessToken = SharedPreferenceUtil.getStringFromSharedPreference(Config.Preferences.AccessToken, "");
        }
        return accessToken;

    }

    public static String getImei() {
        if (TextUtils.isEmpty(imei)) {
            try {
                TelephonyManager tm = (TelephonyManager) SdkApplication.app().getSystemService(Context.TELEPHONY_SERVICE);
                imei = tm.getDeviceId();
            } catch (Exception e) {

            }


        }
        return imei;

    }

    /**
     * mac地址
     *
     * @return
     */

    public static String getMacAddress() {
        if (TextUtils.isEmpty(macAddress)) {
            WifiManager wifi = (WifiManager) SdkApplication.app().getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = wifi.getConnectionInfo();
            if (info != null && !TextUtils.isEmpty(info.getMacAddress())) {
                macAddress = info.getMacAddress();
            }
        }
        return macAddress;

    }


    /**
     * 获取登录的用户名
     *
     * @return
     */

    public static void setFirst() {
        SharedPreferenceUtil.putBooleanByKey(Config.Preferences.ISFIRST, false);
        isFirst = false;

    }

    /**
     * 是否是第一次（更新后）使用
     */
    public static boolean getFirst() {
        if (isFirst == null) {
            isFirst = SharedPreferenceUtil.getBooleanByKey(Config.Preferences.ISFIRST, true);
        }
        return isFirst;
    }


    /**
     * 获取已经保存的最新版本信息
     *
     * @return
     */
    public static AppVersion getNewVersion() {
        if (appNewVersion == null) {
            String str = SharedPreferenceUtil.getStringFromSharedPreference(Config.Preferences.NEWVERSION, "");
            if (!TextUtils.isEmpty(str)) {
                appNewVersion = JsonUtils.fromJson(str, AppVersion.class);
            }
        }
        return appNewVersion;

    }

    /**
     * 设置最新版本信息
     */
    public static void setNewVersionInfo(AppVersion version) {
        SharedPreferenceUtil.putStringValueByKey(Config.Preferences.NEWVERSION, JsonUtils.toJson(version));
        appNewVersion = version;

    }

    /**
     * 最后登录时间
     *
     * @return
     */
    public static long getLastLoginTime() {
        if (lastLoginTime == 0) {
            lastLoginTime = SharedPreferenceUtil.getLongFromSharedPreference(Config.Preferences.LAST_LOGIN_TIME, 0);
        }
        return lastLoginTime;

    }

    /**
     * 设置登录时间
     */
    public static void setLastLoginTime(long t) {
        SharedPreferenceUtil.putLongValueByKey(Config.Preferences.LAST_LOGIN_TIME, t);
        lastLoginTime = t;
    }


    /**
     * 开发模式
     *
     * @return
     */

    public static int getDevModel() {
        if (devModel < 0) {
            devModel = SharedPreferenceUtil.getIntValueByKey(Config.Preferences.DEVMODEL, 0);
        }
        return devModel;

    }

    /**
     * 设置本地IP
     */
    public static void setLocalIp(String ip) {
        SharedPreferenceUtil.putStringValueByKey(Config.Preferences.LOCALIP, ip);
    }

    /**
     * 获取本地ip
     *
     * @return
     */
    public static String getLocalIp() {

        return SharedPreferenceUtil.getStringFromSharedPreference(Config.Preferences.LOCALIP, "127.0.0.1");

    }


    /**
     * 设置环境
     */
    public static void setDevModel(int model) {

        SharedPreferenceUtil.putIntValueByKey(Config.Preferences.DEVMODEL, model);
        devModel = model;
    }

    public static void clearUserName() {
        userName = null;
        setUserName("");

    }

    public static void setPushToken(String push_token) {
        SharedPreferenceUtil.putStringValueByKey(Config.Preferences.PUSH_TOKEN, push_token);
        pushToken = push_token;
    }

    public static String getPushToken() {

        if (TextUtils.isEmpty(pushToken)) {
            pushToken = SharedPreferenceUtil.getStringFromSharedPreference(Config.Preferences.PUSH_TOKEN, "");
        }
        return pushToken;

    }


    //本地测试地址
    public static void setLocalApi(String localApi) {
        SharedPreferenceUtil.putStringValueByKey("localApi", localApi);
    }

    public static String getLoalApi() {
        return SharedPreferenceUtil.getStringFromSharedPreference("localApi", "http://127.0.0.1:8080");
    }

    //本地测试地址
    public static void setLocalBanner(String bannerAdr) {
        SharedPreferenceUtil.putStringValueByKey("localBanner", bannerAdr);
    }

    public static String getLoalBanner() {
        return SharedPreferenceUtil.getStringFromSharedPreference("localBanner", Global.defaultLocalBanner);
    }


    //定位地址
    public static void setMapLocation(String jsonMapLocation) {
        SharedPreferenceUtil.putStringValueByKey("MapLocation", jsonMapLocation);
    }

    public static MyLocaction getMapLocation() {
        String jsonLocation = SharedPreferenceUtil.getStringFromSharedPreference("MapLocation", "");
        if (!TextUtils.isEmpty(jsonLocation)) {
            return JsonUtils.fromJson(jsonLocation, MyLocaction.class);
        } else {
            return null;
        }
    }

    public static String getUserAgentClient() {
        if (userAgentClient == null) {
            userAgentClient = "Mozilla/5.0 (Linux; Android "+android.os.Build.VERSION.RELEASE+"; "+userAgentName+"-android-" + android.os.Build.VERSION.RELEASE + "-app-" + UpdateManager.getVersionName()+"-client) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/44.0.2403.119 Mobile Safari/537.36 ";
        }
        return userAgentClient;
    }

    public static String getUserAgentWebView() {
        if (userAgentWebView == null) {
            userAgentWebView = "Mozilla/5.0 (Linux; Android "+android.os.Build.VERSION.RELEASE+"; "+userAgentName+"-android-" + android.os.Build.VERSION.RELEASE + "-app-" + UpdateManager.getVersionName()+"-webview) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/44.0.2403.119 Mobile Safari/537.36 ";
        }
        return userAgentWebView;
    }

}
